<template>
  <div id="simulator-container">
    <div id="loading-overlay" :class="{ hidden: !isLoading }">
      <div class="loading-text">{{ loadingText }}</div>
    </div>
    <iframe
      :src="iframeSrc"
      frameborder="0"
      allowfullscreen
      allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
      loading="lazy"
      @load="handleIframeLoad"
      @error="handleIframeError"
    ></iframe>
  </div>
</template>

<script>
export default {
  name: 'SimulatorFrame',
  data() {
    return {
      isLoading: true,
      loadingText: '正在加载阵容模拟...',
      iframeSrc: 'https://jcc.qq.com/act/a20220802jksimulator/#/PageSimulatorSCCS2Control'
    }
  },
  mounted() {
    // 监听窗口大小变化
    window.addEventListener('resize', this.handleResize)
  },
  beforeUnmount() {
    // 组件卸载时移除事件监听
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    handleIframeLoad() {
      this.isLoading = false
    },
    handleIframeError() {
      this.loadingText = '加载失败，请刷新页面重试'
      this.isLoading = true
    },
    handleResize() {
      const iframe = document.querySelector('iframe')
      if (iframe) {
        iframe.style.width = window.innerWidth + 'px'
        iframe.style.height = window.innerHeight + 'px'
      }
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#simulator-container {
  width: 100%;
  height: 100%;
  position: relative;
}

iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

#loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #1a0e30 0%, #2e1a4d 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #ffffff;
  z-index: 10;
  transition: opacity 0.5s ease;
}

#loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.loading-text {
  font-size: 18px;
  margin-top: 20px;
  color: #d4af37;
}
</style>